package com.panshi.controller.qa;

import com.panshi.api.*;
import com.panshi.utils.GetSessionUser;
import com.panshi.vo.UserVO;
import com.panshi.vo.comm.TimeList;
import com.panshi.vo.qa.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class ModelQA {

    @Autowired
    ITitleAip titleAip;

    @Autowired
    IQuestionApi questionApi;

    @Autowired
    IReplyApi replyApi;

    @Autowired
    ICommentApi commentApi;

    @Autowired
    IMessagApi messagApi;

    @GetMapping("/answers/main/{toothType}/{tid}")
    public String answersMain(Map<String, Object> model, @PathVariable() Integer toothType,
                              @PathVariable() Integer tid){

        //获取登录后的用户id
        int loginUserId = GetSessionUser.getSessionUserId();
        UserVO loginUser = GetSessionUser.getSessionUser();

        Title title = new Title();
        title.setType(2);
        title.setTstate(1);
        DataResultQA<List<Title>> dataResultQA = titleAip.queryTitle(title);
        List<Title> list = dataResultQA.getData();


        Pages<Question> pages = new Pages<>();
        pages.setStartPage(1);
        pages.setPageSize(5);
        pages.setType(tid);
        pages.setState(1);
        DataResultQA<Pages<Question>> d = questionApi.inquirePagesQuestion(pages);
        pages = d.getData();
        //时间转换
        String[] array = {"getQOkDate"};
        pages.setList(new TimeList<Question>().switchTimeList(d.getData().getList(),array));

        Pages<Question> p = new Pages<>();
        p.setStartPage(1);
        p.setPageSize(10);
        p.setState(1);
        p.setOrder(1);//按阅读次数排序
        DataResultQA<Pages<Question>> d2 = questionApi.inquirePagesQuestion(p);
        Pages<Question> p2 = d2.getData();
        List<Question> listQuestion = p2.getList();

        log.info("list:{}",list);
        log.info("pages:{}",pages);

        if(loginUser != null){
            //获取该用户的站内消息数量
            int mesgCount =messagApi.inquireSum(loginUser.getU_id()).getData();
            model.put("mesgCount",mesgCount);
        }

        model.put("listTitle",list);
        model.put("pages",pages);
        model.put("listQuestion",listQuestion);

        model.put("toothType",toothType);
        model.put("tid",tid);
        model.put("loginUserId",loginUserId);
        model.put("loginUser",loginUser);

        //表示是问答
        model.put("type","qa");

        return "answers/answers_main" ;
    }

    @GetMapping("/answers/particulars/{qid}")
    public String answersParticulars(Map<String, Object> model,@PathVariable() Integer qid){

        //获取登录后的用户id
        int userId = GetSessionUser.getSessionUserId();
        UserVO loginUser = GetSessionUser.getSessionUser();

        //单个问题查询
        DataResultQA<Question> dataResultQA = questionApi.inquireByIdQuestion(qid);
        Question question = dataResultQA.getData();
        //时间转换
        String[] arr = {"getQOkDate"};
        question = new TimeList<Question>().switchTime(question,arr);

        //这个问题的分类id
        Integer tid = question.getQTitleId();
        //回答查询
        Pages p = new Pages<>();
        p.setStartPage(1);
        p.setPageSize(5);
        p.setState(1);
        p.setQuestionId(qid);
        DataResultQA<Pages<Reply>> d1 = replyApi.inquirePagesReply(p);
        List<Reply> listReply = d1.getData().getList();
        //时间转换
        String[] array = {"getROkDate"};
        listReply = new TimeList<Reply>().switchTimeList(listReply,array);

        //问题查询
        Pages<List<Question>> p1 = new Pages<>();
        p1.setStartPage(1);
        p1.setPageSize(5);
        p1.setState(1);
        p1.setOrder(1);//按阅读次数排序
        p1.setType(tid);//分类id
        DataResultQA<Pages<Question>> d2 = questionApi.inquirePagesQuestion(p1);
        Pages<Question> pq = d2.getData();
        List<Question> listQ = pq.getList();

        //问题查询
        Pages p2 = new Pages<>();
        p2.setStartPage(1);
        p2.setPageSize(10);
        p2.setState(1);
        p2.setOrder(1);//按阅读次数排序
        List<Question> listQuestion = questionApi.inquirePagesQuestion(p2).getData().getList();
        //分类查询
        Title title = new Title();
        title.setType(2);
        title.setTstate(1);
        DataResultQA<List<Title>> dt = titleAip.queryTitle(title);
        List<Title> list = dt.getData();

        if(loginUser != null){
            //获取该用户的站内消息数量
            int mesgCount =messagApi.inquireSum(loginUser.getU_id()).getData();
            model.put("mesgCount",mesgCount);
        }

        model.put("question",question);
        model.put("listReply",listReply);
        model.put("listQ",listQ);
        model.put("listQuestion",listQuestion);
        model.put("listTitle",list);
        model.put("toothType",11);
        model.put("tid",tid);
        model.put("userId",userId);
        model.put("loginUser",loginUser);
        //表示是问答
        model.put("type","qa");

        return "answers/answers_particulars";
    }

    /**
     * 分页查询
     * @param pages
     * @return
     */
    @PostMapping("/reply/inquirePagesReplyHtml")
    String inquirePagesReply(Map<String, Object> model,@RequestBody Pages pages){
        DataResultQA<Pages<Reply>> dataResultQA = replyApi.inquirePagesReply(pages);
        List<Reply> listReply = dataResultQA.getData().getList();
        //时间转换
        String[] array = {"getROkDate"};
        listReply = new TimeList<Reply>().switchTimeList(listReply,array);
        model.put("listReply",listReply);
        return "answers/list_reply_data";
    }

    /**
     * 分页查询评论
     * @param pages
     * @return
     */
    @PostMapping("/comment/inquirePagesDataHtml")
    String inquirePagesCommentHtml(Map<String, Object> model, @RequestBody Pages pages){
        DataResultQA<Pages<Comment>> dataResultQA = commentApi.inquirePagesComment(pages);
        List<Comment> listComment = dataResultQA.getData().getList();
        //时间转换
        String[] array = {"getCDate"};//获取时间的get方法
        listComment = new TimeList<Comment>().switchTimeList(listComment,array);

        model.put("listComment",listComment);
        return "/answers/list_comment_data";
    }

    /**
     * 通过id查询评论
     * @param id
     * @return
     */
    @PostMapping("/comment/inquireCommentByIdHtml/{id}")
    String inquireCommentById(Map<String, Object> model,@PathVariable int id){
        DataResultQA<Comment> dataResultQA = commentApi.inquireCommentById(id);
        List<Comment> listComment = new ArrayList<>();
        listComment.add(dataResultQA.getData());
        //时间转换
        String[] array = {"getCDate"};//获取时间的get方法
        listComment = new TimeList<Comment>().switchTimeList(listComment,array);

        model.put("listComment",listComment);
        return "/answers/list_comment_data";
    }

    /**
     * 分页查询评论的回复
     * @param pages
     * @return
     */
    @PostMapping("/comment/inquirePagesRevertDataHtml")
    String inquirePagesRevertHtml(Map<String, Object> model,@RequestBody Pages pages){
        DataResultQA<Pages<Comment>> dataResultQA = commentApi.inquirePagesRevert(pages);
        List<Comment> listComment = dataResultQA.getData().getList();
        //时间转换
        String[] array = {"getCDate"};//获取时间的get方法
        listComment = new TimeList<Comment>().switchTimeList(listComment,array);

        model.put("listComment",listComment);
        return "/answers/list_comment_revert_data";
    }

    /**
     * 通过id查询回复
     * @param id
     * @return
     */
    @PostMapping("/comment/inquireRevertByIdHtml/{id}")
    String inquireRevertById(Map<String, Object> model,@PathVariable int id){
        DataResultQA<Comment> dataResultQA = commentApi.inquireRevertById(id);
        List<Comment> listComment = new ArrayList<>();
        listComment.add(dataResultQA.getData());
        //时间转换
        String[] array = {"getCDate"};//获取时间的get方法
        listComment = new TimeList<Comment>().switchTimeList(listComment,array);

        model.put("listComment",listComment);
        return "/answers/list_comment_revert_data";
    }
}
